<template>
	<view class="comp-box">
		<!-- 添加轮播图 -->
		<view class="part1">
			<view class="title-box u-flex u-row-between">
				<text class="title-text">轮播图</text>
			</view>
			<view class="part1-con">
				<view class="img-con">
					<text class="img-title">上传轮播图 <text class="num-text">（{{swipList.length}}/{{allNum}}）</text></text>
					<u-upload @on-remove="removeSwip" :showProgress="false" @on-uploaded="uploadSuccessSwi"
						:action="`https://job.ydylmold.cn/base/upload/file`" :fileList="acFileSwi"
						:formData="{fileCode:145,fileType:100,fileParaName:'file'}" width="300rpx" height="240rpx"
						:maxCount="allNum">
					</u-upload>
				</view>
			</view>
		</view>
	</view>
</template>
<script>
	import {
		queryPicList,
		queryUsrMainPicNum,
		deleteUsrMainPic,
		insertUsrMainPic,
	} from '@/common/api/auth.js'
	import {
		getToken
	} from '@/utils/token.js'
	export default {
		data() {
			return {
				acFileSwi: [],
				sendSwip: [],
				swipList: [],
				allNum:1
			}
		},
		onLoad: function() {
			this.init()
		},
		methods: {
			uploadSuccessSwi(e) {
				this.sendSwip = []
				e.forEach((v,i) => {
					if (v.response && v.response.code == 200 && i+1>this.swipList.length) {
						this.sendSwip.push(v.response.data)
					}
				})
				this.saveSwip()
			},
			saveSwip() { //保存上传的轮播图
				this.sendSwip.forEach(async m => {
					let result = await this.loadImg(m)
					if (result.data && result.data.code == 1) {
						let acItem = result.data && result.data.data
						this.swipList.push(acItem)
					} else {
						this.$u.toast('上传失败')
					}
				})
			},
			removeSwip(e, list, index, delItem) { //删除上传的轮播图
				this.acFileSwi = list
				let acPath = ''
				if (delItem.response && delItem.response.code == 200) {
					acPath = delItem.response.data
				} else {
					acPath = delItem.url
				}
				let idx2 = this.swipList.findIndex(v => {
					return acPath.includes(v.picPath) || acPath == v.picPath
				})
				if (idx2 != -1) {
					deleteUsrMainPic({
						picId: this.swipList[idx2].picId
					}).then(res => {
						this.swipList.splice(e, 1)
					})
				}
			},
			loadImg(path) {
				return insertUsrMainPic({
					state: '1',
					moduleNo: 'ydylapp1001',
					companyNo: getToken('useNum'),
					isGeneralize: 1,
					imagePic: path
				})
			},
			// 初始化数据
			init() {
				//获取企业轮播图、轮播图数量、企业认证状态、企业更多资料
				Promise.all([this.queryPicList(), this.queryUsrMainPicNum()
				]).then(([
					res1, res2
				]) => {
					if (res1.statusCode == 200) {
						this.swipList = res1.data
						res1.data && res1.data.forEach(m => {
							m.showImg =
								'https://www.ydylmold.cn/images/ydylapp/usrMainPic/usrCompanyImage/' + m
								.picPath
							this.acFileSwi.push({
								url: 'https://www.ydylmold.cn/images/ydylapp/usrMainPic/usrCompanyImage/' +
									m.picPath
							})
						})
					}
					if (res2.data && res2.data.code == 1) {
						this.allNum =res2.data && res2.data.map && res2.data.map.picNum
					}
				})
			},
			//查找轮播图
			queryPicList() {
				if(getToken('useNum'))
				return queryPicList({
					companyNo: getToken('useNum'),
					moduleNo: 'ydylapp1001',
					isType: 0
				})
			},
			// 轮播图梳数量
			queryUsrMainPicNum() {
				return queryUsrMainPicNum({
					companyNo: getToken('useNum'),
					moduleNo: 'ydylapp1001'
				})
			},
		
		}
	}
</script>

<style lang="scss">
	@import "./css/edit.scss";
</style>
